﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
namespace _0712502_0712504_DesignPatternGame
{
    public class AutomatedTankManager
    {
        public bool _isFinished = false;
        private int timeToCreateNewTank = 0;

        private int numOfEnemyTank;
        private int numOfAvailableTank; // Số lượng tank đang có trên màn chơi
        private int maxEnemyTankAvailable; // Số lượng tank tối đa cho mỗi màn chơi
        private int numOfRemainingTank; // Số lượng tank còn lại
        private int numOfGate;
        private int[] enemyTankHps;
        private int[] numOfRemainingTanks = new int[8];
        private int[,] positionAppears; // Vị trí xuất hiện các enemy tank
      
       
        static public List<AutomatedTank> _listComTanks;    // Danh sách các com tank trong màn chơi
        AutomatedTankFactory comTankFactory;
        public AutomatedTankManager()
        {
            _listComTanks = new List<AutomatedTank>();
            comTankFactory = new AutomatedTankFactory();
        }

        public void Intitilize()
        {
            comTankFactory.InitPrototypes();
        }
        public void InitilizeForNewStage()  // Khoi tao cho man choi moi
        {
            numOfRemainingTank = GlobalVariables.numOfEnemyTank;    // So luong enemy tank trong man choi
            numOfRemainingTanks = GlobalVariables.numOfEnemyTanks; // So luong tank cac loai
            maxEnemyTankAvailable = GlobalVariables.maxEnemyTankAvailable;  // 
            positionAppears = GlobalVariables.gatePostions;
            enemyTankHps = GlobalVariables.enemyTankHPs;
            numOfGate = GlobalVariables.numOfGate;
        }

        public void Update(GameTime gameTime)
        {            
            numOfAvailableTank = _listComTanks.Count();

            for (int i = 0; i < _listComTanks.Count(); i++)
            {
                _listComTanks.ElementAt(i).TankUpdate(gameTime);

                if (_listComTanks.ElementAt(i).StateUsed == StateInUse.NotUsed)
                {
                    numOfAvailableTank -= 1;
                    _listComTanks.RemoveAt(i);
                }
            }
            if (numOfRemainingTank > 0)
            {
                AddNewTankToGameStage();
            }
            else
            {
                if (numOfAvailableTank == 0)
                {
                    this._isFinished = true;
                }
            }
            
           
        }

        public void AddNewTankToGameStage()
        {
            timeToCreateNewTank++;
            if (timeToCreateNewTank > 100)
            {
                if (numOfAvailableTank < maxEnemyTankAvailable &&
                    numOfRemainingTank > 0)
                {                  
                    Random rand = new Random(DateTime.Now.Millisecond);
                    int randType = rand.Next(1, 100);
                    int randPos;
                    randType = (randType % 4) + 1;

                    bool addSuccess = false;                    
                    while (addSuccess == false)
                    {
                        if (randType == 1)
                        {
                            if (numOfRemainingTanks[0] > 0)
                            {
                                AutomatedTank1 tank1 = (AutomatedTank1)comTankFactory.CreateObject(0);
                                                                                                
                                randPos = rand.Next(1, 100);
                                randPos = randPos % numOfGate;
                                tank1.Position = new Vector2(positionAppears[randPos,0],positionAppears[randPos,1]);
                                tank1.MaxHP = 1000;
                                tank1.CurrentHP = tank1.MaxHP;
                                tank1.ExpMark = 200;
                               // if (tank1.CheckDungDoLucXuatHien == false)
                                {
                                    _listComTanks.Add(tank1);
                                    numOfRemainingTanks[0] -= 1;
                                    addSuccess = true;                                   
                                }
                                break;
                            }
                            else
                            {
                                randType = 2;
                            }                          
                        }

                        if (randType == 2)
                        {
                            if (numOfRemainingTanks[1] > 0)
                            {
                                AutomatedTank2 tank2 = (AutomatedTank2)comTankFactory.CreateObject(1);
                  
                                randPos = rand.Next(1, 100);
                                randPos = randPos % numOfGate;
                                tank2.Position = new Vector2(positionAppears[randPos, 0], positionAppears[randPos, 1]);
                                tank2.MaxHP = 1000;
                                tank2.CurrentHP = tank2.MaxHP;
                                tank2.ExpMark = 200;
                                // if (tank1.CheckDungDoLucXuatHien == false)
                                {
                                    _listComTanks.Add(tank2);
                                    numOfRemainingTanks[1] -= 1;
                                    addSuccess = true;
                                }
                                break;
                            }

                            else
                            {
                                randType = 3;
                            }
                        }

                        if (randType == 3)
                        {
                            if (numOfRemainingTanks[2] > 0)
                            {
                                AutomatedTank3 tank3 = (AutomatedTank3)comTankFactory.CreateObject(2);

                                randPos = rand.Next(1, 100);
                                randPos = randPos % numOfGate;
                                tank3.Position = new Vector2(positionAppears[randPos, 0], positionAppears[randPos, 1]);
                                tank3.MaxHP = 1000;
                                tank3.CurrentHP = tank3.MaxHP;
                                tank3.ExpMark = 200;
                                // if (tank1.CheckDungDoLucXuatHien == false)
                                {
                                    _listComTanks.Add(tank3);
                                    numOfRemainingTanks[2] -= 1;
                                    addSuccess = true;
                                }
                                break;
                            }
                            else
                            {
                                randType = 4;
                            }
                        }
                        if (randType == 4)
                        {
                            if (numOfRemainingTanks[3] > 0)
                            {
                                AutomatedTank4 tank4 = (AutomatedTank4)comTankFactory.CreateObject(3);

                                randPos = rand.Next(1, 100);
                                randPos = randPos % numOfGate;
                                tank4.Position = new Vector2(positionAppears[randPos, 0], positionAppears[randPos, 1]);
                                tank4.MaxHP = 1000;
                                tank4.CurrentHP = tank4.MaxHP;
                                tank4.ExpMark = 200;
                                // if (tank1.CheckDungDoLucXuatHien == false)
                                {
                                    _listComTanks.Add(tank4);
                                    numOfRemainingTanks[3] -= 1;
                                    addSuccess = true;

                                }
                                break;
                            }
                            else
                            {
                                randType = 1;
                            }
                        }                                                    
                    }
                    if (addSuccess == true)
                    {
                        numOfAvailableTank += 1;
                        numOfRemainingTank -= 1;
                        GlobalVariables.numOfEnemyTank = numOfRemainingTank;
                    }                            
                }
                timeToCreateNewTank = 0;
            }
        }

        
        public void DrawToScreen(GameTime gameTime, SpriteBatch spriteBatch)
        {
            foreach (AutomatedTank comTank in _listComTanks)
            {
                comTank.DrawToScreen(gameTime, spriteBatch);
            }
        }
    }
}
